﻿using System;
using Gpf.Whs.YouTubeDownloader.AddIn.CommSrv;
using Microsoft.WindowsServerSolutions.Web;
using System.Web.UI.WebControls;

namespace Gpf.Whs.YouTubeDownloader.AddIn
{
    public partial class Default : ContentPage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            System.Web.UI.ScriptManager _scriptMan = System.Web.UI.ScriptManager.GetCurrent(this);
            _scriptMan.EnablePartialRendering = true;

            loadFormatsCommand.Click += LoadFormatsCommandClick;
            downloadCommand.Click += DownloadCommandClick;
            progressTimer.Tick += ProgressTimerTick;
        }

        private void ProgressTimerTick(object sender, EventArgs e)
        {
            LoadDownloadStatuses();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            errorField.Text = string.Empty;

            if (IsPostBack)
                return;

            ShowStep1();
            LoadMediaLibraries();
        }

        protected void CancelDownloadCommandClick(object sender, RepeaterCommandEventArgs e)
        {
            using (var client = GetClient())
            {
                client.CancelDownload(e.CommandArgument.ToString());
            }
        }

        private void LoadFormatsCommandClick(object sender, EventArgs e)
        {            
            try
            {
                using (var client = GetClient())
                {
                    var result = client.GetFormats(videoUrlField.Text);
                    videoTitleField.Text = result.Title;
                    formatsList.DataSource = result.Formats;
                    formatsList.DataBind();

                    ShowStep2();
                }
            }
            catch (Exception ex)
            {
                errorField.Text += "Error: " + ex.ToString();
            }
        }

        private void LoadMediaLibraries()
        {
            try
            {
                using (var client = GetClient())
                {
                    var result = client.GetSharedMediaFolders();
                    librariesList.DataSource = result;
                    librariesList.DataBind();
                }
            }
            catch (Exception ex)
            {
                errorField.Text = "Error: " + ex.ToString();
            }
        }

        private void ShowStep1()
        {
            titleRow.Visible = false;
            libraryRow.Visible = false;
            formatRow.Visible = false;
        }

        private void ShowStep2()
        {
            titleRow.Visible = true;
            libraryRow.Visible = true;
            formatRow.Visible = true;
        }

        private void DownloadCommandClick(object sender, EventArgs e)
        {
            try
            {
                using (var client = GetClient())
                {
                    client.DownloadFile(videoUrlField.Text, Guid.Parse(librariesList.SelectedItem.Value), byte.Parse(formatsList.SelectedItem.Value));
                    videoUrlField.Text = string.Empty;
                    formatsList.Items.Clear();
                    errorField.Text = "Video is queued for download";
                    ShowStep1();
                    LoadDownloadStatuses();
                }
            }
            catch (Exception ex)
            {
                errorField.Text += "Error: " + ex.ToString();
            }
        }

        private void RefreshProgressCommandClick(object sender, EventArgs e)
        {
            LoadDownloadStatuses();
        }

        private void LoadDownloadStatuses()
        {
            progressErrorField.Text = string.Empty;

            try
            {
                using (var client = GetClient())
                {
                    var statuses = client.GetItemStatuses();
                    downloadProgressRepeater.DataSource = statuses;
                    downloadProgressRepeater.DataBind();
                }
            }
            catch (Exception ex)
            {
                progressErrorField.Text = ex.ToString();
            }
        }

        private YouTubeDownloaderCommSrvClient GetClient()
        {
            return CommSrvHelper.GetClient();
        }
    }
}
